/*[[ Name := EMAknife Author := Copyright © 2005, Robert Cochran/Automatic Forex Link := http://autoforex.biz Notes := Use H4 timeframe. Modifications ------------- DATE MOD # DESCRIPTION ---------- -------- ------------------------------------------------------------ 11 Jun 2005 1 Autofx: Original version Update on every tick := Yes Enable Alerts := Yes Disable alert once hit := No Lots := 1 Stop Loss := 50 Take Profit := 350 Trailing Stop := 0 ]]*/ // ==================================================================================================== // DECLARATION AND ASSIGNMENT // ==================================================================================================== defines: TrailStop(1),CloseViaStoch(1),StochDelta(5); defines: Risk(3),mm(1),LiveTrading(0),AccountIsMini(0); defines: Slippage(3),maxTradesPerPair(1); vars: MarginCutoff(0),lotMM(0),sl(0),tp(0),getOut(0),target(0),cnt(0),CurrentTrades(0); vars: Psar(0); vars: StochEntry(""),valueStochEntry(0); Psar = iSAR(0.6, 0.2, 0); // specify a name for the global variable StochEntry = AccountNumber+"_"+Symbol+"_"+Period+"_StochEntry"; // define the variable if it doesn't already exist if NOT IsGlobalVariable(StochEntry) then SetGlobalVariable(StochEntry, 0); // get a value valueStochEntry = GetGlobalVariable(StochEntry); getOut = StopLoss * Point; target = TakeProfit * Point; // ==================================================================================================== // DATE CHOKE - For testing purposes // ==================================================================================================== //if Year <> 2005 or Month <> 5 then Exit; // ==================================================================================================== // WAIT TEN SECONDS BETWEEN ALL TRADING ACTIONS // ==================================================================================================== if CurTime - LastTradeTime < 10 Then Exit; // ==================================================================================================== // PYRAMIDING - LINEAR // Money management risk exposure compounding // ==================================================================================================== if AccountIsMini == 0 then MarginCutoff = 1000; if AccountIsMini == 1 then MarginCutoff = 100; if FreeMargin < Margincutoff then Exit; if mm <> 0 then { lotMM = Ceil(Balance * risk / 10000) / 10; if lotMM < 0.1 then lotMM = Lots; if lotMM > 1.0 then lotMM = Ceil(lotMM); // Enforce lot size boundaries if LiveTrading == 1 then { if AccountIsMini == 1 then lotMM = lotMM * 10; if AccountIsMini == 0 and lotMM < 1.0 then lotMM = 1.0; } if lotMM > 100 then lotMM = 100; } else { lotMM = Lots; // Change mm to 0 if you want the Lots parameter to be in effect }; // ==================================================================================================== // OPEN ORDER CHECK - // Each instance of a script is attached to one currency pair. // When this check executes, it sets CurrentTrades to 1 so that // only one trade for this symbol will be open, which is enforced // by "if CurrentTrades = 0". // ==================================================================================================== CurrentTrades = 0; for cnt = 1 to TotalTrades { if OrderValue(cnt,VAL_SYMBOL) = Symbol then { CurrentTrades += 1; }; }; // ==================================================================================================== // ORDER HANDLING // ==================================================================================================== for cnt = 1 to TotalTrades { if CurrentTrades <> 0 and OrderValue(cnt,VAL_SYMBOL) = Symbol then { if OrderValue(cnt,VAL_TYPE) = OP_BUY then { Comment("Stoch Entry ", valueStochEntry, "\nCurrent Stoch: ", iSTO(5,5,3,MODE_EMA,MODE_SIGNAL,0), "\nLong Target ", valueStochEntry + StochDelta); if TrailStop == 1 and Bid > Ord(cnt,VAL_OPENPRICE) + 30*Point and Psar > OrderValue(cnt,VAL_STOPLOSS) and Psar < Bid then { ModifyOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_OPENPRICE), Psar, OrderValue(cnt,VAL_TAKEPROFIT), BLUE); Exit; } if CloseViaStoch == 1 and iSTO(5,5,3,MODE_EMA,MODE_SIGNAL,0) >= valueStochEntry + StochDelta then { //Comment("LONG at ", OrderValue(cnt,VAL_OPENPRICE), "\nClosed at: ", Bid); CloseOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_LOTS), Bid, Slippage, Violet); Exit; } }; // end OP_BUY check if OrderValue(cnt,VAL_TYPE) = OP_SELL then { Comment("Stoch Entry ", valueStochEntry, "\nCurrent Stoch: ", iSTO(5,5,3,MODE_EMA,MODE_SIGNAL,0), "\nShort Target ", valueStochEntry - StochDelta); if TrailStop == 1 and Ask < Ord(cnt,VAL_OPENPRICE) - 30*Point and Psar < OrderValue(cnt,VAL_STOPLOSS) and Psar > Ask then { ModifyOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_OPENPRICE), Psar, OrderValue(cnt,VAL_TAKEPROFIT), BLUE); Exit; } if CloseViaStoch == 1 and iSTO(5,5,3,MODE_EMA,MODE_SIGNAL,0) <= valueStochEntry - StochDelta then { //Comment("SHORT at ", OrderValue(cnt,VAL_OPENPRICE), "\nClosed at: ", Ask); CloseOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_LOTS), Ask, Slippage, Violet); Exit; } }; // end OP_SELL check }; // end Symbol check }; // end for cnt=1 to TotalTrades // ==================================================================================================== // TRADE ENTRY // ==================================================================================================== if CurrentTrades < maxTradesPerPair then { //LONG TRADES ENTRY CRITERIA if iMA(3,MODE_EMA,0) > iMA(8,MODE_EMA,0) and Ask < iMA(3,MODE_EMA,0) and Ask < Low[1] then { // set global variable's value SetGlobalVariable(StochEntry, iSTO(5,5,3,MODE_EMA,MODE_SIGNAL,0)); sl = Ask - getOut; tp = Ask + target; SetOrder(OP_BUY, lotMM, Ask, Slippage, sl, tp, LIME); //Comment("LONG at ", Ask, "\nStop: ", sl, "\nLimit: ", tp); Exit; }; //SHORT TRADES ENTRY CRITERIA if iMA(3,MODE_EMA,0) < iMA(8,MODE_EMA,0) and Bid > iMA(3,MODE_EMA,0) and Bid > High[1] then { // set global variable's value SetGlobalVariable(StochEntry, iSTO(5,5,3,MODE_EMA,MODE_SIGNAL,0)); sl = Bid + getOut; tp = Bid - target; SetOrder(OP_SELL, lotMM, Bid, Slippage, sl, tp, RED); //Comment("SHORT at ", Bid, "\nStop: ", sl, "\nLimit: ", tp); Exit; }; }; // end of if CurrentTrades < maxTradesPerPair